/*
 * @Author: 刘强
 * @Date: 2022-10-31 15:35:40
 * @LastEditTime: 2022-10-31 16:04:55
 * @LastEditors: 刘强
 * @Description:
 * @FilePath: \Library\src\api\bug.ts
 * 这是注释!
 */
import Api from "./index";
import { browserVersion } from "../util/browser";
import { timeFormat } from "../util/time";
import { localeStore } from "../store/local/index";
import { deleteInvalidParam } from "../util/convert";

const pingList: Array<string> = []; //网速耗时队列
const browser = browserVersion(); // 浏览器信息
// 网络连接方式 不准确
let effectiveType = "";

(() => {
  if ((navigator as any).connection === undefined) {
    effectiveType = "该浏览器暂不支持";
  } else {
    effectiveType = (navigator as any).connection.effectiveType;
  }
})();
// 会议中参数
const meetObj = {
  roomId: "", // 房间id
  peerId: [], // 操作目标
  typeName: "", // 更新的设备-摄像头/麦克风
};
// 接口列表-耗时
const httpsData = {};

// 会议列表-会议id
const meetId = [];

// 基础格式
function getData() {
  return {
    浏览器类型: browser[0],
    浏览器版本: browser[1],
    网络类型: effectiveType, // 连接方式
    是否登录: "", //用户是否登录
    用户手机号: localeStore.userInfo.userId,
    操作时间: timeFormat(new Date()), // 默认格式2021-06-29 17:21:25
    消耗时间: "", // 网络请求-params.time
    操作类型: "服务端接口", // im-服务端接口
    接口名称: "", // https接口名称
    操作名称: "", // 删除会议
    操作结果CODE: "", // 状态码0为成功其他为失败
    提示消息: "", // message
  };
}
// 上传日志
export function sendClientLog(data) {
  try {
    if (data.message == null || data.message === "" || data.message === undefined) return;
    data.message = JSON.stringify(data.message);
    data.message = data.message.replace(/\\/g, "");
    data.message = data.message.replace(/\//g, "");
    // eslint-disable-next-line no-useless-escape
    data.message = data.message.replace(/\"/g, "");
    Api.upLog(data);
    // 清除本地日志-
    if (data.level === "INFO") {
      localeStore.setLogList([]);
    }
  } catch (error) {
    console.log(error);
  }
}
// https埋点-响应-异常
export function buryingPointResponse(resp) {
  try {
    const { code, message } = resp.data;
    const url = resp.config.url.replace(new RegExp("mock/", "gm"), "");
    const urlName = url.replace(new RegExp("/", "gm"), "");
    const httpsTime = new Date().getTime() - httpsData[urlName]; // 时差
    // 添加请求耗时计算网速
    if (pingList.length > 30) pingList.splice(0, 1);
    pingList.push(httpsTime.toString());
    // code === 0 ||
    if (code === 0 || url == "aps/client/log") return;
    // const data = resp.data.data || {}; // 响应参数
    const params = getData();

    params.操作类型 = "http服务端接口";
    params.接口名称 = url;
    params.消耗时间 = `${httpsTime}ms/毫秒`;
    params.操作结果CODE = code;
    params.提示消息 = message;
    switch (url) {
      // 注册
      case "aps/user/register":
        params.操作名称 = "注册";
        break;
      // 注销
      case "aps/user/delete":
        params.操作名称 = "注销";
        break;
      // 短信登录
      case "aps/login/sms":
        params.操作名称 = "短信登录";
        break;
      // 获取短信验证码
      case "aps/user/dynamicCode":
        params.操作名称 = "获取短信验证码";
        break;
      // 扫码登录
      case "aps/login/qrCodeToken":
        params.操作名称 = "扫码登录";
        break;
      // 更新用户状态
      case `aps/meeting/control/${meetObj.roomId}/member/config`:
        params.操作名称 = "更新用户";
        break;
      default:
        return;
    }

    const resData = {
      level: code === 0 ? "INFO" : "ERROR",
      message: deleteInvalidParam(params),
    };

    sendClientLog(resData);
  } catch (error) {
    console.log(error);
  }
}
// https埋点-请求
export function buryingPointRequest(resp) {
  try {
    const { url, params } = resp;
    // 埋点http耗时
    const urlName = url.replace(new RegExp("/", "gm"), "");

    httpsData[urlName] = new Date().getTime();
    // 参数拦截

    switch (url) {
      // 更新自己的设备状态
      case `aps/meeting/control/${params.roomId}/member/config`:
        meetObj.roomId = params.roomId;
        meetObj.typeName = params.typeName;
        break;

      default:
    }
  } catch (error) {
    console.log(error);
  }
}
// js错误崩溃埋点-异常
export function buryingPointDIV(data = { error: "无提示消息" }, name = "js错误") {
  try {
    const params = getData();

    params.操作类型 = "网页错误";
    params.操作名称 = name;
    if (typeof data == "string") {
      params.提示消息 = data;
    } else {
      params.提示消息 = JSON.stringify(data);
    }

    let arr = localeStore.logList;

    const newParams = JSON.stringify(deleteInvalidParam(params));

    arr.push(newParams);
    localeStore.setLogList(arr);
    // 日志数量少于30条不提交-页面关闭前必须提交一次日志
    if (arr.length < 30) return;

    const resData = {
      level: "INFO",
      message: arr.join("\n"),
    };

    sendClientLog(resData);
  } catch (error) {}
}
// js日常记录
export function buryingPointJS(data = { info: "" }, name = "js错误") {
  try {
    const params = getData();

    params.操作类型 = "js执行记录";
    params.操作名称 = name;
    if (typeof data == "string") {
      params.提示消息 = data;
    } else {
      params.提示消息 = JSON.stringify(data);
    }
    let arr = localeStore.logList;
    const newParams = JSON.stringify(deleteInvalidParam(params));

    arr.push(newParams);
    localeStore.setLogList(arr);
    // 日志数量少于30条不提交-页面关闭前必须提交一次日志
    if (arr.length < 30) return;

    const resData = {
      level: "INFO",
      message: arr.join("\n"),
    };

    sendClientLog(resData);
  } catch (error) {}
}

// 主动提交本地缓存日志-
export function buryingPointReduxManual() {
  try {
    let arr = localeStore.logList;
    if (arr.length < 1) return;
    localeStore.setLogList(arr);
    const resData = {
      level: "INFO",
      message: arr.join("\n"),
    };

    sendClientLog(resData);
  } catch (error) {}
}
